#!/usr/bin/perl

# Written by Johan Mulder <johan@localhost.nl>
# Modified by Sten Spans <sten@blinkenlights.nl>
#
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <sten@blinkenlights.nl> wrote this module. As long as you retain this notice
# you can do whatever you want with this stuff. If we meet some day, and you
# think this stuff is worth it, you can buy me a beer in return.
# Sten Spans
# ----------------------------------------------------------------------------
#
# copy this to your nagios mail account dir and make a .forward file like 
# this:
# (nagios@r8):~
# $ cat .forward
# \nagios
# "|~/nagios-udp"
#
# edit config vars below to match your bot's config

use Socket;

$host = 'localhost';
$port = 65536;
$passwd = 'test';
$channel = '#foo';

$ip   = inet_aton($host);

while (<STDIN>)
{
   next unless /^subject:/i;
	
   s/^subject:(\s+)//i;

   s/(OK)/\003$1\003/g;		s/(OK)/9$1/g;
   s/(UP)/\003$1\003/g;		s/(UP)/9$1/g;
   s/(WARNING)/\003$1\003/g;	s/(WARNING)/8$1/g;
   s/(UNKNOWN)/\003$1\003/g;	s/(UNKNOWN)/8$1/g;
   s/(CRITICAL)/\003$1\003/g;	s/(CRITICAL)/4$1/g;
   s/(DOWN)/\003$1\003/g;	s/(DOWN)/4$1/g;

   socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname("udp"));

   $msg = "$passwd $channel $_";

   send(SOCKET, $msg, 0, sockaddr_in($port, $ip)) == length($msg) 
        or die "cannot send to $host($port): $!";

   close(SOCKET);
	
   exit(0);
}

